package com.example.wendao.model.db.dao;

import androidx.lifecycle.LiveData;
import androidx.paging.DataSource;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.example.wendao.model.entity.Forum;
import com.example.wendao.model.entity.News;

import java.util.List;

@Dao
public interface NewsDao {

    //插入新闻
    @Insert
    void insert(News news);

    //获得其他新闻
    @Query("select * from News where newsId not in (select newsId from News order by newsAgree desc limit 0, 5)")
    DataSource.Factory<Integer, News> findOtherAllNews();

    @Query("select * from News")
    DataSource.Factory<Integer, News> findAllNews();

    //根据点赞数获得当前热度最高的新闻
    @Query("select * from News order by newsAgree desc limit 0, 5")
    LiveData<List<News>> findHeatNews();

    // 查找最新提交的一个新闻
    @Query("select * from news order by newsId desc limit 0, 1")
    News findLatestNews();

    //根据多个帖子id查找帖子
    /**
     * param:forumids
     * return: DataSource.Factory<Integer, Forum>
     */
    @Query("select * from News where newsId in (:newsids)")
    DataSource.Factory<Integer, News> findSomeNewsByNewsIds(List<Integer> newsids);

    //查询标题带有某字样的新闻
    @Query("SELECT * FROM News where NewsId like '%' || :title || '%'")
    DataSource.Factory<Integer, News> findNewsByTitle(String title);

    //按照用户id搜索其名下新闻
    @Query("SELECT * FROM news where newsAuthorId = :newsauthorid")
    DataSource.Factory<Integer, News> findAllNewsByAuthorId(int newsauthorid);

    //根据新闻id查询新闻
    @Query("SELECT * FROM News where NewsId = :id")
    News findNewsById(int id);


    //根据新闻类型查询新闻
    @Query("SELECT * FROM News where newsType = :newstype")
    DataSource.Factory<Integer, News> findNewsByNewsType(int newstype);

    //更新新闻
    @Update
    void updateNews(News... news);

    //删除新闻
    @Delete
    void deleteNews(News... news);

    //按照用户id搜索其名下新闻
    @Query("SELECT * FROM news where newsAuthorId = :newsauthorid")
    News[] findAllNewsByAuthorIdInArray(int newsauthorid);
}
